package com.jackyzgy.test.code;

import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;

public class Base64 {
	
	public static void main(String[] args) {
		String srcString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Deliver>    <GateID>1</GateID> /*由网关填写*/	<MsgID>123456</MsgID>	<DestTermID>aa</DestTermID>/*短消息接收号码(特服号)*/		<SrcTermID>aa</SrcTermID>/*短消息发送号码*/	<SrcTermType>1</SrcTermType>/*短消息发送号码类型 移动协议*/	<ServiceID>aa</ServiceID>/**移动协议上行有业务代码 其他无*/	<MsgContent>fds房顶上</MsgContent>/*短消息内容 base64加密*/	<LinkID></LinkID>/*移动协议*/</Deliver>";
		String desString = null;
		
		//编码
		try {
			desString = base64Encode(srcString);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return;
		}
		System.out.println(desString);
		
		//解码
		desString = base64Decode(desString);
		System.out.println(desString);
	}
	
	private static String base64Encode(String srcString) throws UnsupportedEncodingException{
		if(srcString == null){
			return null; 
		}
		
		return (new sun.misc.BASE64Encoder()).encode(srcString.getBytes("UTF-8"));
	}

	private static String base64Decode(String srcString){
		if (srcString == null){
			return null; 
		}
		
		BASE64Decoder decoder = new BASE64Decoder(); 
		
		try {
			byte[] b = decoder.decodeBuffer(srcString);
			return new String(b,"UTF-8");
		} catch (Exception e) {
			return null;
		}
	}
}
